#!python3
# 外星飞船入侵游戏入口

import pygame
from pygame.sprite import Group

from Setting import Settings
from Ship import Ship
import game_functions as gf  # 导入模块，指定别名

# 如要继续阅读《Python编程：从入门到实践》
# 13.6.2 响应外星飞船和飞船碰撞

def run_game():
    gameSetting = Settings()

    pygame.init()  # 初始化背景设置，让Pygame能够正确地工作
    # 创建一个名为screen的显示窗口,参数为尺寸
    screen = pygame.display.set_mode((gameSetting.screen_width, gameSetting.screen_height))
    pygame.display.set_caption('外星飞船入侵')

    # 创建飞船
    ship = Ship(screen)

    # 创建子弹编组
    bullets = Group()

    # # 创建一个外星飞船
    # alien = Alien(gameSetting, screen)

    # 创建外星飞船编组
    aliens = Group()
    # 创建外星飞船群
    gf.create_fleet(gameSetting, screen, ship, aliens)

    while True:
        # 监听键盘和鼠标事件
        gf.check_events(gameSetting, screen, ship, bullets)

        # 移动飞船
        ship.move(gameSetting)

        # 更新子弹
        gf.update_bullets(gameSetting, screen, ship, bullets, aliens)

        # 更新外星飞船
        gf.update_aliens(gameSetting, ship, aliens)

        # 绘制一个空屏幕，并擦去旧屏幕
        gf.update_screen(gameSetting, screen, ship, bullets, aliens)


if __name__ == '__main__':
    run_game()
